package com.zyl.Ecs.realtime.util;

import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;

/**
 * @version v1.0
 * @ProjectName: Ecs
 * @Package: com.zyl.Ecs.realtime.util
 * @ClassName: TableUtil
 * @Description TODO 动态表格工具类
 * @Author 翟玉龙
 * @Date 2024/12/6-10:52
 */
public class TableUtil {

    /**
     * 表名格式：DATA_{四位数年份}{两位数月份}_{传感器ID}
     */
    private static final String TABLE_NAME_TEMPLATE = "DATA_%d%02d_%s";

    /**
     * 根据指定年、月、传感器ID，生成对应表格名称
     *
     * @param year     年份
     * @param month    月份
     * @param sensorId 传感器ID
     * @return {@link String }
     */
    public static String tableName(int year, int month, Integer sensorId) {
        return String.format(TABLE_NAME_TEMPLATE, year, month, sensorId);
    }

    /**
     * 根据传感器ID，生成对应表格名称
     *
     * @param time 时间
     * @return {@link String }
     */
    public static String tableName(Long time, Integer sensorId) {
        LocalDateTime now = LocalDateTime.ofInstant(Instant.ofEpochMilli(time), ZoneId.systemDefault());
        int year = now.getYear();
        int month = now.getMonthValue();
        return tableName(year, month, sensorId);
    }

    /**
     * 根据传感器ID，生成对应表格名称
     * @param sensorId 传感器ID
     * @return {@link String }
     */
    public static String tableName(Integer sensorId) {
        LocalDateTime now = LocalDateTime.now();
        int year = now.getYear();
        int month = now.getMonthValue();
        return tableName(year, month, sensorId);
    }

}
